﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zor_Modes;
using Zor_IDAL;
using Zor_Tools;

namespace Zor_DAL
{
    public class Zor_Building_DAL<T> where T : class
    {
        /// <summary>
        /// 楼宇管理-修改
        /// </summary>
        /// <param name="building">楼宇对象</param>
        /// <returns>执行结果：1 成功 0失败</returns>
        public static BaseAnswer UpdateBuilding(Zor_Building building)
        {
            var sql = "";
            sql += "update [Zor_Building] set Dt_Id=@Dt_Id, Bd_Name=@Bd_Name,Bd_Ground=@Bd_Ground,Bd_Underground=@Bd_Underground,Bd_Longitude=@Bd_Longitude,Bd_Latitude=@Bd_Latitude,Bd_Structure=@Bd_Structure,Bd_Years=@Bd_Years,Bd_Aname=@Bd_Aname,Bd_Sex=@Bd_Sex,Bd_IDNumber=@Bd_IDNumber,Bd_Telephone=@Bd_Telephone  where Bd_Id = @Bd_Id";
            BaseAnswer bar = DapperEdit<T>.DoEdit(sql, building);
            return bar;
        }
        /// <summary>
        /// 楼宇管理-新增
        /// </summary>
        /// <param name="building">楼宇对象</param>
        /// <returns>执行结果：1 成功 0失败</returns>
        public static BaseAnswer InsertBuilding(Zor_Building building)
        {
            var sql = @"if not exists(select Bd_Name from [Zor_Building] where Bd_Name =@Bd_Name ) 
                        insert into Zor_Building values 
                        (@Dt_Id,@Bd_Name,@Bd_Number,@Bd_Ground,@Bd_Underground,@Bd_Longitude,@Bd_Latitude,@Bd_Structure,@Bd_Years,@Bd_Aname,@Bd_Sex,@Bd_IDNumber,@Bd_Telephone,@Bd_PhotoPath,@Bd_CreateDate,@User_Id,@Bd_Flage)";

            BaseAnswer bar = DapperInsert<T>.DoInsert(sql, building);
            return bar;
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id">Id集合</param>
        /// <returns></returns>
        public static BaseAnswer DeleteBuilding(int[] id)
        {
            var sql = @"if not exists(select Uim_Id from [Zor_UnitInfor] where BD_Id in (" + string.Join(",", id).TrimEnd(',') + ")) delete [Zor_Building]  where  Bd_Id in(" + string.Join(",", id).TrimEnd(',') + ")";
                        
            BaseAnswer bar = DapperDelete<T>.DoDelete(sql, new { Bd_Id = id.ToArray<int>() });
            return bar;
        }

        /// <summary>
        /// 获取楼宇下拉列表
        /// </summary>
        /// <returns></returns>
        public static List<T> GetBuildingList()
        {
            var dt = RequestSession.GetSessionUser().DT_Id;
            var sql = "select Bd_Id,Bd_Name from dbo.Zor_Building where Bd_Flage = 2 and Dt_Id = " + dt + " order by Bd_Name ";
            List<T> listBuilding = DapperQuery<T>.DoQueryList(sql, null);
            return listBuilding;
        }

        /// <summary>
        /// 获取楼宇下拉列表
        /// </summary>
        /// <returns></returns>
        public static List<T> GetListAddress()
        {
            var dt = RequestSession.GetSessionUser().DT_Id;
            var sql = "select [Dt_Name] from [dbo].[Sys_Department] where Dt_Id in (select Dt_ParentId  from dbo.Sys_Department where Dt_Id = " + dt + ") ";
            List<T> listBuilding = DapperQuery<T>.DoQueryList(sql, null);
            return listBuilding;
        }
        /// <summary>
        /// 获取楼宇下拉列表
        /// </summary>
        /// <returns></returns>
        public static List<T> GetListSq()
        {
            var dt = RequestSession.GetSessionUser().DT_Id;
            var sql = "select [Dt_Name] from [dbo].[Sys_Department]  where Dt_Id = " + dt + " ";
            List<T> listBuilding = DapperQuery<T>.DoQueryList(sql, null);
            return listBuilding;
        }

        /// <summary>
        /// 获取所属部门名称
        /// </summary>
        /// <returns></returns>
        public static List<T> GetDtName()
        {
            var sql = "select Dt_Id,Dt_Name from dbo.Sys_Department where Dt_Id = "+RequestSession.GetSessionUser().DT_Id+" ";
            List<T> listDtName = DapperQuery<T>.DoQueryList(sql, null);
            return listDtName;
        }

        /// <summary>
        /// 获取楼宇管理列表（不分页）
        /// create 刘云飞 by 2017/04/06   
        /// </summary>
        /// <returns></returns>
        public static List<T> SelectViewBuilding(string where = null)
        {
            string strwhere = "";
            if (!string.IsNullOrEmpty(where))
            {
                strwhere = "1=1 and " + where;
            }
            var sql = "select * from [dbo].[V_Zor_Building] where " + strwhere + " and Dt_Id = " + RequestSession.GetSessionUser().DT_Id + " order by Bd_Id ";
            return DapperQuery<T>.DoQueryList(sql, null);
        }


        /// <summary>
        /// 获取楼宇管理列表（不分页）
        /// create 刘云飞 by 2017/04/06   
        /// </summary>
        /// <returns></returns>
        public static List<T> SelectViewStreet(string where = null)
        {
            string strwhere = "";
            if (!string.IsNullOrEmpty(where))
            {
                strwhere = "1=1 and " + where;
            }
            var sql = "select * from [dbo].[V_Zor_Building] where " + strwhere + " and Dt_Id = " + RequestSession.GetSessionUser().DT_Id + " order by Bd_Id ";
            return DapperQuery<T>.DoQueryList(sql, null);
        }


    }
}
